package StackUse;

import java.util.Arrays;

public class MyStack {

    private int[] elem;
    private int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    //放入元素
    public void push(int val) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    public int pop(){
        //1、判断栈不为空
        if (empty()) {
            throw new StackEmptyException("栈为空");
        }
        //2、删除
        return elem[--usedSize];
    }

    public int peek(){
        //1、判断栈不为空
        if (empty()) {
            throw new StackEmptyException("栈为空");
        }
        //2、删除
        return elem[usedSize-1];
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public boolean empty() {
        if (usedSize == 0) {
            return true;
        }
        return false;
    }

}
